//
// SoapTrace.Net - A soap message monitoring utility. 
//
// Copyright (C) 2004 Matthew Ward
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// Matthew Ward (mrward@users.sourceforge.net)

using System;
using System.Collections;

namespace SoapTrace.Core.Diagnostics
{
	/// <summary>
	/// A collection that stores a finite number of <see cref='LogEntry'/> 
	/// objects.
	/// </summary>
	[Serializable()]
	public class LogEntryCollection : CollectionBase {
		
		/// <summary>
		/// The default maximum number of items in this 
		/// <see cref="LogEntryCollection"/>.
		/// </summary>
		public const int DefaultCountLimit = 10;
		
		/// <summary>
		/// The maximum number of items in this <see cref="LogEntryCollection"/>.
		/// </summary>
		private int m_CountLimit = LogEntryCollection.DefaultCountLimit;
		
		/// <summary>
		///   Initializes a new instance of <see cref='LogEntryCollection'/>.
		/// </summary>
		public LogEntryCollection()
		{
		}
		
		/// <summary>
		///   Initializes a new instance of <see cref='LogEntryCollection'/> based on another <see cref='LogEntryCollection'/>.
		/// </summary>
		/// <param name='val'>
		///   A <see cref='LogEntryCollection'/> from which the contents are copied
		/// </param>
		public LogEntryCollection(LogEntryCollection val)
		{
			this.AddRange(val);
		}
		
		/// <summary>
		///   Initializes a new instance of <see cref='LogEntryCollection'/> containing any array of <see cref='LogEntry'/> objects.
		/// </summary>
		/// <param name='val'>
		///       A array of <see cref='LogEntry'/> objects with which to intialize the collection
		/// </param>
		public LogEntryCollection(LogEntry[] val)
		{
			this.AddRange(val);
		}
		
		/// <summary>
		/// Gets or sets the maximum number of items allowed in this collection.
		/// </summary>
		public int CountLimit
		{
			get
			{
				return m_CountLimit;
			}
			
			set
			{
				m_CountLimit = value;
			}
		}
		
		/// <summary>
		///   Represents the entry at the specified index of the <see cref='LogEntry'/>.
		/// </summary>
		/// <param name='index'>The zero-based index of the entry to locate in the collection.</param>
		/// <value>The entry at the specified index of the collection.</value>
		/// <exception cref='ArgumentOutOfRangeException'><paramref name='index'/> is outside the valid range of indexes for the collection.</exception>
		public LogEntry this[int index] {
			get {
				return ((LogEntry)(List[index]));
			}
			set {
				List[index] = value;
			}
		}
	
		/// <summary>
		///   Adds a <see cref='LogEntry'/> with the specified value to the 
		///   <see cref='LogEntryCollection'/>.
		/// </summary>
		/// <param name='val'>The <see cref='LogEntry'/> to add.</param>
		/// <returns>The index at which the new element was inserted.</returns>
		/// <seealso cref='LogEntryCollection.AddRange'/>
		public int Add(LogEntry val)
		{
			int Index = -1;
			
			if( m_CountLimit > 0 )
			{							
				// Remove extra entries.
				
				while(  List.Count >= m_CountLimit )
				{
					List.RemoveAt( 0 );
				}
				
				Index = List.Add(val);
			}
			
			return Index;
		}
		
		/// <summary>
		///   Copies the elements of an array to the end of the <see cref='LogEntryCollection'/>.
		/// </summary>
		/// <param name='val'>
		///    An array of type <see cref='LogEntry'/> containing the objects to add to the collection.
		/// </param>
		/// <seealso cref='LogEntryCollection.Add'/>
		public void AddRange(LogEntry[] val)
		{
			for (int i = 0; i < val.Length; i++) {
				this.Add(val[i]);
			}
		}
		
		/// <summary>
		///   Adds the contents of another <see cref='LogEntryCollection'/> to the end of the collection.
		/// </summary>
		/// <param name='val'>
		///    A <see cref='LogEntryCollection'/> containing the objects to add to the collection.
		/// </param>
		/// <seealso cref='LogEntryCollection.Add'/>
		public void AddRange(LogEntryCollection val)
		{
			for (int i = 0; i < val.Count; i++)
			{
				this.Add(val[i]);
			}
		}
		
		/// <summary>
		///   Gets a value indicating whether the 
		///    <see cref='LogEntryCollection'/> contains the specified <see cref='LogEntry'/>.
		/// </summary>
		/// <param name='val'>The <see cref='LogEntry'/> to locate.</param>
		/// <returns>
		/// <see langword='true'/> if the <see cref='LogEntry'/> is contained in the collection; 
		///   otherwise, <see langword='false'/>.
		/// </returns>
		/// <seealso cref='LogEntryCollection.IndexOf'/>
		public bool Contains(LogEntry val)
		{
			return List.Contains(val);
		}
		
		/// <summary>
		///   Copies the <see cref='LogEntryCollection'/> values to a one-dimensional <see cref='Array'/> instance at the 
		///    specified index.
		/// </summary>
		/// <param name='array'>The one-dimensional <see cref='Array'/> that is the destination of the values copied from <see cref='LogEntryCollection'/>.</param>
		/// <param name='index'>The index in <paramref name='array'/> where copying begins.</param>
		/// <exception cref='ArgumentException'>
		///   <para><paramref name='array'/> is multidimensional.</para>
		///   <para>-or-</para>
		///   <para>The number of elements in the <see cref='LogEntryCollection'/> is greater than
		///         the available space between <paramref name='arrayIndex'/> and the end of
		///         <paramref name='array'/>.</para>
		/// </exception>
		/// <exception cref='ArgumentNullException'><paramref name='array'/> is <see langword='null'/>. </exception>
		/// <exception cref='ArgumentOutOfRangeException'><paramref name='arrayIndex'/> is less than <paramref name='array'/>'s lowbound. </exception>
		/// <seealso cref='Array'/>
		public void CopyTo(LogEntry[] array, int index)
		{
			List.CopyTo(array, index);
		}
		
		/// <summary>
		///    Returns the index of a <see cref='LogEntry'/> in 
		///       the <see cref='LogEntryCollection'/>.
		/// </summary>
		/// <param name='val'>The <see cref='LogEntry'/> to locate.</param>
		/// <returns>
		///   The index of the <see cref='LogEntry'/> of <paramref name='val'/> in the 
		///   <see cref='LogEntryCollection'/>, if found; otherwise, -1.
		/// </returns>
		/// <seealso cref='LogEntryCollection.Contains'/>
		public int IndexOf(LogEntry val)
		{
			return List.IndexOf(val);
		}
		
		/// <summary>
		///   Inserts a <see cref='LogEntry'/> into the <see cref='LogEntryCollection'/> at the specified index.
		/// </summary>
		/// <param name='index'>The zero-based index where <paramref name='val'/> should be inserted.</param>
		/// <param name='val'>The <see cref='LogEntry'/> to insert.</param>
		/// <seealso cref='LogEntryCollection.Add'/>
		public void Insert(int index, LogEntry val)
		{
			List.Insert(index, val);
		}
		
		/// <summary>
		///  Returns an enumerator that can iterate through the <see cref='LogEntryCollection'/>.
		/// </summary>
		/// <seealso cref='IEnumerator'/>
		public new LogEntryEnumerator GetEnumerator()
		{
			return new LogEntryEnumerator(this);
		}
		
		/// <summary>
		///   Removes a specific <see cref='LogEntry'/> from the <see cref='LogEntryCollection'/>.
		/// </summary>
		/// <param name='val'>The <see cref='LogEntry'/> to remove from the <see cref='LogEntryCollection'/>.</param>
		/// <exception cref='ArgumentException'><paramref name='val'/> is not found in the Collection.</exception>
		public void Remove(LogEntry val)
		{
			List.Remove(val);
		}
		
		/// <summary>
		///   Enumerator that can iterate through a LogEntryCollection.
		/// </summary>
		/// <seealso cref='IEnumerator'/>
		/// <seealso cref='LogEntryCollection'/>
		/// <seealso cref='LogEntry'/>
		public class LogEntryEnumerator : IEnumerator
		{
			IEnumerator baseEnumerator;
			IEnumerable temp;
			
			/// <summary>
			///   Initializes a new instance of <see cref='LogEntryEnumerator'/>.
			/// </summary>
			public LogEntryEnumerator(LogEntryCollection mappings)
			{
				this.temp = ((IEnumerable)(mappings));
				this.baseEnumerator = temp.GetEnumerator();
			}
			
			/// <summary>
			///   Gets the current <see cref='LogEntry'/> in the <seealso cref='LogEntryCollection'/>.
			/// </summary>
			public LogEntry Current {
				get {
					return ((LogEntry)(baseEnumerator.Current));
				}
			}
			
			object IEnumerator.Current {
				get {
					return baseEnumerator.Current;
				}
			}
			
			/// <summary>
			///   Advances the enumerator to the next <see cref='LogEntry'/> of the <see cref='LogEntryCollection'/>.
			/// </summary>
			public bool MoveNext()
			{
				return baseEnumerator.MoveNext();
			}
			
			/// <summary>
			///   Sets the enumerator to its initial position, which is before the first element in the <see cref='LogEntryCollection'/>.
			/// </summary>
			public void Reset()
			{
				baseEnumerator.Reset();
			}
		}
	}
}
